package cn.dongqihong.hehe;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

public class HttpRequestUtil {
	/**
	 * UTF-8编码
	 */
	public static final String CHARSET_UTF8 = "UTF-8";
	/**
	 * GBK编码
	 */
	public static final String CHARSET_GBK = "GBK";
	/**
	 * GB2312编码
	 */
	public static final String CHARSET_GB2312 = "GB2312";

	/**
	 * 测试
	 * 
	 */
	public static void main(String[] args) throws Exception {
		String s = HttpRequestUtil.get("http://www.sina.com.cn",
				HttpRequestUtil.CHARSET_GBK, true);

		System.out.println(s);

	}

	/**
	 * 发起get请求
	 * 
	 * @param urlStr
	 *            请求的URL地址
	 * @param charset
	 *            字符集
	 * @param pretty
	 *            是否美化
	 * @return 返回请求响应的HTML
	 */
	public static String get(String urlStr, String charset, boolean pretty) {
		URL url;
		InputStream in = null;
		StringBuffer html = new StringBuffer();
		try {
			url = new URL(urlStr);
			in = url.openStream();
			BufferedReader bin = new BufferedReader(new InputStreamReader(in,
					charset));
			String line;
			while ((line = bin.readLine()) != null) {
				if (pretty) {
					html.append(line).append(
							System.getProperty("line.separator"));
				} else {
					html.append(line);
				}
			}
			in.close();
			bin.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return html.toString();
	}

	/**
	 * 发起post请求
	 * 
	 * @param url
	 *            请求的URL地址
	 * @param params
	 *            请求参数
	 * @param charset
	 *            字符集
	 * @param pretty
	 *            是否美化
	 * @return 返回请求响应的HTML
	 */
	public static String post(String url, Map<String, String> params,
			String charset, boolean pretty) {
		URL u = null;
		String paramString = null;
		HttpURLConnection con = null;
		// 构建请求参数
		StringBuffer sb = new StringBuffer();
		if (params != null) {
			for (Entry<String, String> e : params.entrySet()) {
				sb.append(e.getKey());
				sb.append("=");
				sb.append(e.getValue());
				sb.append("&");
			}
			paramString = sb.substring(0, sb.length() - 1);
		}
		// 尝试发送请求
		try {
			u = new URL(url);
			con = (HttpURLConnection) u.openConnection();
			con.setRequestMethod("POST");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setUseCaches(false);
			con.setRequestProperty("Connection", "Keep-Alive");// 维持长连接   
			con.setRequestProperty("Charset", charset); 
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			OutputStreamWriter osw = new OutputStreamWriter(
					con.getOutputStream(), charset);
			osw.write(paramString);
			osw.flush();
			osw.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				con.disconnect();
			}
		}

		// 读取返回内容
		StringBuffer buffer = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					con.getInputStream(), "UTF-8"));
			String temp;
			while ((temp = br.readLine()) != null) {
				buffer.append(temp);
				buffer.append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return buffer.toString();
	}

}